﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TCM.Controllers
{
    public class RegistrationController : Controller
    {
        private TCMContext db = new TCMContext();

        //
        // GET: /Registration/

        public ActionResult Index()
        {
            var Registrations = db.Registrations
                .Include(s => s.Subject)
                //.Include(s => s.MAPSTRINGBASE)
.Include(s => s.Student);//.Include(s => s.User);
            return View(Registrations.ToList());
        }

        //
        // GET: /Registration/Details/5

        public ActionResult Details(Guid? id = null)
        {
           var  Registration = db.Registrations.Find(id);
            if (Registration == null)
            {
                return HttpNotFound();
            }
            return View(Registration);
        }

        //
        // GET: /Registration/Create

        public ActionResult Create()
        {
            ViewBag.SubjectID = new SelectList(db.Subjects, "SubjectID", "NAME");
           // ViewBag.StatusID = new SelectList(db.MAPSTRINGBASEs, "MAPSTRINGID", "DISPLAYVALUE");
            ViewBag.StudentID = new SelectList(db.Students, "StudentID", "NAME");
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "UserName");
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName");
            return View();
        }

        //
        // POST: /Registration/Create

        [HttpPost]
        public ActionResult Create(Registration Registration)
        {
            if (ModelState.IsValid)
            {
               // Registration.RegistrationID= Guid.NewGuid();
                db.Registrations.Add(Registration);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.SubjectID = new SelectList(db.Subjects, "SubjectID", "NAME", Registration.SubjectID);
           // ViewBag.StatusID = new SelectList(db.MAPSTRINGBASEs, "MAPSTRINGID", "DISPLAYVALUE", Registration.StatusID);
            ViewBag.StudentID = new SelectList(db.Students, "StudentID", "NAME", Registration.StudentID);
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "NAME", Registration.OwnerID);

            ViewBag.StatusID = new SelectList(db.RecordStates.ToList().AsEnumerable(), "StatusID", "StatusName", Registration.StudentID);

            return View(Registration);
        }

        //
        // GET: /Registration/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            var Registration = db.Registrations.Find(id);
            if (Registration == null)
            {
                return HttpNotFound();
            }
            ViewBag.SubjectID = new SelectList(db.Courses, "SubjectID", "NAME", Registration.SubjectID);
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName", Registration.StatusID);
            ViewBag.StudentID = new SelectList(db.Students, "StudentID", "NAME", Registration.StudentID);
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "UserName", Registration.OwnerID);
           // Registration.students = Registration.Student (p => p.StudentID).AsEnumerable();
            return View(Registration);
        }

        //
        // POST: /Registration/Edit/5

        [HttpPost]
        public ActionResult Edit(Registration Registration)
        {
            if (ModelState.IsValid)
            {
                db.Entry(Registration).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SubjectID = new SelectList(db.Courses, "SubjectID", "NAME", Registration.SubjectID);
            ViewBag.StatusID = new SelectList(db.RecordStates
                .ToList().AsEnumerable(), "StatusID", "StatusName", Registration.StatusID);
            ViewBag.StudentID = new SelectList(db.Students, "StudentID", "NAME", Registration.StudentID);
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "NAME", Registration.OwnerID);
            return View(Registration);
        }


       


        //
        // GET: /Registration/Delete/5

        public ActionResult Delete(Guid? id = null)
        {
          var Registration = db.Registrations.Find(id);
            if (Registration == null)
            {
                return HttpNotFound();
            }
            return View(Registration);
        }

        //
        // POST: /Registration/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
          var Registration = db.Registrations.Find(id);
            db.Registrations.Remove(Registration);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}